# Definition for singly-linked list.
from collections import deque
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        v_head = ListNode(-1, head)
        prev = v_head
        curr = head
        while curr is not None:
            if curr.val == val:
                prev.next = curr.next
            else:
                prev = curr
            curr = curr.next
        return v_head.next


if __name__ == '__main__':
    s = Solution()
    v = [7, 7, 7, 7]
    val = 7
    nodes = deque()
    for i in range(len(v) - 1, -1, -1):
        if i < len(v) - 1:
            node = ListNode(v[i], nodes[0])
        else:
            node = ListNode(v[i], None)
        nodes.appendleft(node)
    r = s.removeElements(nodes[0], val)
    while r:
        print(r.val, end=' ')
        r = r.next
